package com.ikun.aimian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikun.aimian.common.ErrorCode;
import com.ikun.aimian.exception.ThrowUtils;
import com.ikun.aimian.mapper.InterviewPostMapper;
import com.ikun.aimian.model.dto.interview.InterviewPostQueryRequest;
import com.ikun.aimian.model.entity.InterviewPost;
import com.ikun.aimian.model.entity.User;
import com.ikun.aimian.model.vo.InterviewPostVO;
import com.ikun.aimian.service.InterviewPostService;
import com.ikun.aimian.service.UserService;
import com.ikun.aimian.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class InterviewPostServiceImpl extends ServiceImpl<InterviewPostMapper, InterviewPost> implements InterviewPostService {

    @Resource
    private UserService userService;

    @Override
    public void validPost(InterviewPost post, boolean add) {
        ThrowUtils.throwIf(post == null, ErrorCode.PARAMS_ERROR);
        String title = post.getTitle();
        String content = post.getContent();
        String difficulty = post.getDifficulty();
        if (add) {
            ThrowUtils.throwIf(StringUtils.isBlank(title), ErrorCode.PARAMS_ERROR, "标题不能为空");
            ThrowUtils.throwIf(StringUtils.isBlank(content), ErrorCode.PARAMS_ERROR, "内容不能为空");
            ThrowUtils.throwIf(StringUtils.isBlank(difficulty), ErrorCode.PARAMS_ERROR, "难度不能为空");
        }
        if (title != null && title.length() > 256) {
            ThrowUtils.throwIf(true, ErrorCode.PARAMS_ERROR, "标题过长");
        }
        if (content != null && content.length() > 10000) {
            ThrowUtils.throwIf(true, ErrorCode.PARAMS_ERROR, "内容过长");
        }
    }

    @Override
    public QueryWrapper<InterviewPost> getQueryWrapper(InterviewPostQueryRequest queryRequest) {
        QueryWrapper<InterviewPost> queryWrapper = new QueryWrapper<>();
        if (queryRequest == null) return queryWrapper;
        String title = queryRequest.getTitle();
        Long userId = queryRequest.getUserId();
        if (StringUtils.isNotBlank(title)) {
            queryWrapper.like("title", title);
        }
        if (userId != null) {
            queryWrapper.eq("userId", userId);
        }
        String sortField = queryRequest.getSortField();
        String sortOrder = queryRequest.getSortOrder();
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equalsIgnoreCase("ascend"), sortField);
        return queryWrapper;
    }

    @Override
    public InterviewPostVO getPostVO(InterviewPost post, HttpServletRequest request) {
        InterviewPostVO vo = InterviewPostVO.objToVo(post);
        Long userId = post.getUserId();
        if (userId != null) {
            User user = userService.getById(userId);
            if (user != null) {
                vo.setUser(userService.getUserVO(user));
            }
        }
        return vo;
    }

    @Override
    public Page<InterviewPostVO> getPostVOPage(Page<InterviewPost> postPage, HttpServletRequest request) {
        List<InterviewPost> postList = postPage.getRecords();
        Page<InterviewPostVO> voPage = new Page<>(postPage.getCurrent(), postPage.getSize(), postPage.getTotal());
        if (postList == null || postList.isEmpty()) {
            return voPage;
        }
        List<InterviewPostVO> voList = postList.stream()
                .map(post -> getPostVO(post, request)) // 用 Lambda，把 request 传进去
                .collect(Collectors.toList());
        voPage.setRecords(voList);
        return voPage;
    }
}
